/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.chartextensions.fitter;

import JFreeChartExtensions.ChartPanelDialogs.SelectReggresionDialog;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.Fitters.ChartFit;
import JFreeChartExtensions.chartextensions.IChartExtensionManager;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChart;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Nobody
 */
public class ReggresionFittingManager implements KeyListener, ChartMouseListener, IChartExtensionManager {

    private boolean isControlPressed;
    private final ChartFit chartFit = new ChartFit();
    private ExtendableChartPanel chartPanel;

    public ReggresionFittingManager(ExtendableChartPanel chartPanel) {
        this.chartPanel = chartPanel;
    }

    @Override
    public void keyPressed(java.awt.event.KeyEvent evt) {
        isControlPressed = evt.isControlDown();
    }

    @Override
    public void keyReleased(java.awt.event.KeyEvent evt) {
        isControlPressed = false;
        chartFit.setxEnd(null);
        chartFit.setxStart(null);
    }

    @Override
    public void chartMouseClicked(ChartMouseEvent cme) {
        if (!isControlPressed) {
            return;
        }
        Point2D p = cme.getTrigger().getPoint();
        Rectangle2D plotArea = chartPanel.getScreenDataArea();
        XYPlot xyplot = (XYPlot) cme.getChart().getPlot();
        double x = xyplot.getDomainAxis().java2DToValue(p.getX(), plotArea, xyplot.getDomainAxisEdge());
        if (chartFit.isXStartDefined() && !chartFit.isXEndDefined()) {
            System.out.println("set end");
            chartFit.setxEnd(x);
        } else if(!chartFit.areBoundsDefined()){
            System.out.println("set start");
            chartFit.setxStart(x);
        }
         if(chartFit.areBoundsDefined()) {
            try {
                System.out.println("fitting");
                chartFit.sortBounds();
                double start = chartFit.getxStart();
                double end = chartFit.getxEnd();
                chartFit.reset();
                isControlPressed = false;
                openSelectReggresionDialog(chartPanel, start, end);
            } catch (InterruptedException ex) {
                Logger.getLogger(InternalFrameWithChart.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvocationTargetException ex) {
                Logger.getLogger(InternalFrameWithChart.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void chartMouseMoved(ChartMouseEvent cme) {
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    private void openSelectReggresionDialog(final ExtendableChartPanel chart, final Double x, final Double y) throws InterruptedException, InvocationTargetException {

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new SelectReggresionDialog(chart, x, y).setVisible(true);
            }
        });
    }

    @Override
    public void register(ChartPanel chartPanel) {
        this.chartPanel = (ExtendableChartPanel) chartPanel;
        chartPanel.addKeyListener(this);
        chartPanel.addChartMouseListener(this);
    }

    @Override
    public void unregister() {
        chartPanel.removeKeyListener(this);
        chartPanel.removeChartMouseListener(this);
    }
}
